package com.dbb.trade.entity.order;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.dbb.trade.entity.IdEntity;
import com.dbb.trade.entity.product.Product;
import com.dbb.trade.entity.shipment.Ctn;

@Entity
@Table(name = "ORDER_ITEM")
public class OrderItem extends IdEntity {
	private Product product;
	private Float unitPrice;
	private List<ItemComment> descs = new ArrayList<ItemComment>();
	private Ctn ctns;
	private String frontMark;
	private String sideMark;
	private Integer totalPairs;
	
	public OrderItem() { }
	
	public OrderItem(Product product, Ctn ctns, Integer totalPairs, String... des) {
		this.product = product;
		this.ctns = ctns;
		int length = des.length;
		if(length%2 == 1) {
			length--;
		}
		for(int i = 0; i < length; i += 2) {
			descs.add(new ItemComment(des[i], des[i + 1]));
		}
		this.totalPairs = getRealPairs(ctns.getPrs(), totalPairs);
	}
	
	public OrderItem(Product product, Ctn ctns, Integer totalPairs) {
		this.product = product;
		this.ctns = ctns;
		this.totalPairs = totalPairs;
		this.totalPairs = getRealPairs(ctns.getPrs(), totalPairs);
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "PRODUCT_ID")
	public Product getProduct() {
		return product;
	}
	public void setProduct(Product product) {
		this.product = product;
	}
	
	@OneToOne
	@JoinColumn(name = "ITEM_ID")
	public Ctn getCtns() {
		return ctns;
	}
	public void setCtns(Ctn ctns) {
		this.ctns = ctns;
	}

	public Float getUnitPrice() {
		return unitPrice;
	}
	public void setUnitPrice(Float unitPrice) {
		this.unitPrice = unitPrice;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@JoinColumn(name = "O_ID")
	public List<ItemComment> getDescs() {
		return descs;
	}

	public void setDescs(List<ItemComment> descs) {
		this.descs = descs;
	}

	public String getFrontMark() {
		return frontMark;
	}

	public void setFrontMark(String frontMark) {
		this.frontMark = frontMark;
	}

	public String getSideMark() {
		return sideMark;
	}

	public void setSideMark(String sideMark) {
		this.sideMark = sideMark;
	}
	
	public Integer getTotalPairs() {
		return totalPairs;
	}
	
	@Transient
	public Integer getTotalPack() {
		return totalPairs / ctns.getPrs();
	}
	
	@Transient
	public Float getTotalPrice() {
		return getTotalPairs() * unitPrice;
	}

	public void setTotalPairs(Integer totalPairs) {
		this.totalPairs = totalPairs;
	}
	
	@Transient
	private Integer getRealPairs(Integer ctnPrs, Integer totalPairs) {
		int ctnNum = totalPairs/ctnPrs;
		if(ctnNum * ctnPrs < totalPairs) {
			return (ctnNum + 1) * ctnPrs;
		}
		return ctnNum * ctnPrs;
	}
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
	}
}
